package com.ddwl.data.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.data.service.ProtocolConfigService;
import com.ddwl.schema.bo.base.BaseBo;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.data.ProtocolConfigBo;
import com.ddwl.schema.bo.data.QueryProtocolConfigBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.data.ProtocolConfigVo;
import com.ddwl.schema.vo.data.ProtocolTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @Author wenbin
 * @Date 2020/11/10 16:34
 */
@RestController
@RequestMapping("/protocolConfig")
@Api(tags = "用户协议")
public class ProtocolConfigController extends BaseController {

    @Resource
    private ProtocolConfigService protocolConfigService;

    @PostMapping("/getProtocolConfig")
    @ApiOperation("获取协议")
    public RestVo<ProtocolConfigVo> getProtocolConfig(@Valid @RequestBody QueryProtocolConfigBo bo) {
        return RestVo.ok(protocolConfigService.getProtocolConfig(bo));
    }

    @PostMapping("/page")
    @ApiOperation("获取协议列表")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<ProtocolConfigVo>> page(@RequestBody PageBo<QueryProtocolConfigBo> pageBo) {
        QueryProtocolConfigBo param = Optional.ofNullable(pageBo.getParam()).orElse(new QueryProtocolConfigBo());
        pageBo.setParam(param);
        return RestVo.ok(protocolConfigService.page(pageBo));
    }

    @GetMapping("/getProtocolTypeList")
    @ApiOperation("获取协议类型列表")
    public RestVo<List<ProtocolTypeVo>> getLogisticsList() {
        List<ProtocolTypeVo> protocolTypeList = protocolConfigService.getProtocolTypeList();
        return RestVo.ok(protocolTypeList);
    }

    @PostMapping("/create")
    @ApiOperation("添加协议")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo create(@Validated(value = BaseBo.Create.class) @RequestBody ProtocolConfigBo bo) {
        protocolConfigService.create(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改协议")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo update(@Validated(value = BaseBo.Update.class) @RequestBody ProtocolConfigBo bo) {
        protocolConfigService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除协议")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo delete(@Validated(value = BaseBo.Delete.class) @RequestBody ProtocolConfigBo bo) {
        protocolConfigService.delete(bo);
        return RestVo.ok();
    }

}
